var api = require('../../utils/api.js');
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        images: []
    },

    deleteimg(ret) {
        console.log(ret);

        var i = ret.currentTarget.dataset.i;
        var images = this.data.images;
        var imgs = [];
        images.forEach(function (item, index) {

            if (index != i) {
                imgs.push(item)
            }

        })
        this.setData({
            images: imgs
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    backs() {
        wx.navigateBack();
    },

    uploadpdf() {

        let that = this;
        var usrl = api.HOST;
        var images = this.data.images;


        wx.chooseMedia({
            count: 9,
            mediaType: ['image', 'video'],
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) {
                console.log(res.tempFiles);
                let tempFilePaths = res.tempFiles;
                tempFilePaths.forEach(function (item) {
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl + '/api/common/upload',
                        formData: {},
                        success: function (res) {
                            var data = JSON.parse(res.data);

                            console.log(data);

                            images.push(data.data.img)
                            that.setData({
                                images: images
                            })

                        },
                        fail: function (res) {
                            console.log('上传失败');
                        }
                    })


                })



            }

        })
    },
    getdesc(ret) {

        console.log(ret);
        this.setData({
            desc: ret.detail.value
        })
    },
    submits() {
        var that = this;
        var desc = this.data.desc;
        var images = this.data.images;
        wx.showLoading({
          title: '提交中...',
        })
        var that = this;
        api.post({
            url: 'dongtai/adddongtai',
            data: {
                desc: desc,
                images: images
            },
            success: data => {

                console.log(data)
                wx.showToast({
                    title: data.msg,
                    icon: 'none'
                })

                wx.switchTab({
                    url: '/pages/my/my',
                })

            }
        })




    }

})